package com.example.secondhandsysapi.Mapper;

import com.example.secondhandsysapi.Entity.BrowseLogInfo;
import com.example.secondhandsysapi.Vo.BrowseGoodsInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.sql.Date;
import java.util.List;

@Mapper
public interface BrowseLogInfoMapper {
    //获取网站一个星期的访问量（即活跃用户数量）
    @Select("SELECT COUNT(DISTINCT user_id) AS active_user_count  \n" +
            "FROM table_browselog_info  \n" +
            "WHERE #{browse_time} >= CURDATE() - INTERVAL 1 WEEK")
    Integer getActiveUserCount(Date browse_time);

    //默认显示当天的浏览记录
    @Select("select browse_id,browse_time,browselog_info.user_id,browselog_info.goods_id,goods_name,goods_price,goods_image,category_id " +
            "from browselog_info join goods_info on browselog_info.goods_id=goods_info.goods_id \n" +
            "where browselog_info.user_id=#{user_id} and date(browse_time)=curdate()")
    List<BrowseGoodsInfo> getBrowseLogInfo(Integer user_id);

    //根据用户ID和商品名称查询
    @Select("select browse_id,browse_time,browselog_info.user_id,browselog_info.goods_id,goods_name,goods_price,goods_image,category_id \n" +
            "from browselog_info join goods_info on browselog_info.goods_id=goods_info.goods_id \n" +
            "where browselog_info.user_id=#{user_id} and goods_name=#{name}")
    List<BrowseGoodsInfo> getBrowseInfoByIdAndName(@Param("user_id")Integer user_id,@Param("name") String name);

    @Select("select browse_id,browse_time,browselog_info.user_id,browselog_info.goods_id,goods_name,goods_price,goods_image,category_id \n" +
            "from browselog_info join goods_info on browselog_info.goods_id=goods_info.goods_id \n" +
            "where browselog_info.user_id=#{user_id} and date(browse_time)=#{date}")
    List<BrowseGoodsInfo> getBrowseInfoByIdAndDate(@Param("user_id")Integer user_id,@Param("date")Date date);

    @Select("select browse_id,browse_time,browselog_info.user_id,browselog_info.goods_id,goods_name,goods_price,goods_image,category_id \n" +
            "from browselog_info join goods_info on browselog_info.goods_id=goods_info.goods_id \n" +
            "where browselog_info.user_id=#{user_id} and date(browse_time)=#{date} and goods_name=#{name}")
    List<BrowseGoodsInfo> getBrowseInfoByIdAndDateAndName(@Param("user_id")Integer user_id,@Param("date")Date date,@Param("name") String name);

    // 浏览详情页的时候插入浏览记录
    @Select("INSERT INTO browselog_info (user_id, goods_id, browse_time) VALUES (#{user_id}, #{goods_id},now())")
    Integer insertBrowseLogInfo(@Param("user_id") Integer user_id,@Param("goods_id") Integer goods_id);
}
